package com.fireline.beauty.cashier;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import com.zgw.fireline.base.controls.DataTable;
import com.zgw.fireline.base.dataset.Dataset;
import com.zgw.fireline.base.frame.FunctionDialog;

import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.wb.swt.SWTResourceManager;
import com.zgw.fireline.base.IDataBaseProvide;
import com.fireline.beauty.bean.HyxfxmVo;
import com.fireline.beauty.jdbc.SqliteJdbcImpl;
import com.zgw.fireline.base.controls.DataTableColumn;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import com.zgw.fireline.base.widgets.Batch;
import com.zgw.fireline.base.widgets.BatchItem;
import com.zgw.fireline.base.widgets.ControlUtil;

/**
 * 结帐挂单
 * */
public class GdDialog extends Dialog {

	protected int result = SWT.NONE;
	protected Shell shell;
	private Text text;
	/**
	 * @wbp.nonvisual location=37,502
	 */
	private final Dataset zdSet = new Dataset(
			"select a.*,b.hyxm from hyxf a\r\nleft join hyxx b on a.hybh=b.hybh\r\n where a.flag=0 and a.hybh=?\r\nand (? is null or a.xfid=?)",
			SqliteJdbcImpl.INSTANCE);
	/**
	 * @wbp.nonvisual location=127,502
	 */
	private final Dataset zdmxSet = new Dataset(
			"\r\nselect a.*,\r\n\tcase\r\n\t when a.lx=1 then\r\n\t'护理消费'\r\n        when a.lx=2 then\r\n\t'产品消费'\r\n        when a.lx=3 then\r\n\t'套餐消费'\t\r\n\tend lxmc,\r\n\tifnull(b.xmmc,c.cpmc) xmmc, -- 项目名称\r\n\tifnull(b.dj,c.sj) xmyj,-- 原价\r\n\ta.cs*a.dj zje\t\t\t-- 总金额\r\n from hyxfnr a \r\nleft join fwxm b on a.lx in (1,3) and bh=b.xmbh\r\nleft join cpxx c on a.lx=2 and bh=c.cpbh\r\nwhere xfid=?",
			SqliteJdbcImpl.INSTANCE);
	/**
	 * @wbp.nonvisual location=23,562
	 */
	private final Batch deleteZdBatch = new Batch(SqliteJdbcImpl.INSTANCE);
	private String hybh;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public GdDialog(Shell parent, String hybh) {
		super(parent, SWT.NONE);
		this.hybh = hybh;
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result for SWT.CHANGED|SWT.NONE
	 */
	public int open() {
		createContents();
		if (zdSet.getCount() <= 0) {
			MessageDialog.openInformation(getParent(), "提示", "该会员当前不存在挂单信息!");
			return SWT.NONE;
		}
		ControlUtil.centerShell(shell);
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		zdSet.setKeyColumns(new String[] {});
		zdmxSet.setKeyColumns(new String[] {});

		BatchItem batchItem = new BatchItem(deleteZdBatch);
		batchItem.setCommand("delete from hyxfnr where xfid=?");
		batchItem.setName("删除消费明细");

		BatchItem batchItem_1 = new BatchItem(deleteZdBatch);
		batchItem_1.setCommand("delete from hyxf where xfid=?");
		batchItem_1.setName("删除消费单");
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.MAX | SWT.RESIZE
				| SWT.APPLICATION_MODAL);
		shell.setSize(588, 378);
		shell.setText("结帐挂单");
		shell.setLayout(new GridLayout(1, false));

		Composite composite = new Composite(shell, SWT.NONE);
		GridLayout gl_composite = new GridLayout(6, false);
		gl_composite.horizontalSpacing = 3;
		composite.setLayout(gl_composite);

		Label label = new Label(composite, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label.setText("帐单号：");

		text = new Text(composite, SWT.BORDER);
		GridData gd_text = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_text.widthHint = 100;
		text.setLayoutData(gd_text);

		Button btnNewButton = new Button(composite, SWT.NONE);
		btnNewButton.setImage(SWTResourceManager.getImage(GdDialog.class,
				"/com/fireline/beauty/image/query.png"));
		GridData gd_btnNewButton = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_btnNewButton.heightHint = 25;
		gd_btnNewButton.widthHint = 80;
		btnNewButton.setLayoutData(gd_btnNewButton);
		btnNewButton.setText("查询");
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doSearch();
			}
		});

		Button btnNewButton_1 = new Button(composite, SWT.NONE);
		GridData gd_btnNewButton_1 = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_btnNewButton_1.horizontalIndent = 10;
		gd_btnNewButton_1.widthHint = 80;
		gd_btnNewButton_1.heightHint = 25;
		btnNewButton_1.setLayoutData(gd_btnNewButton_1);
		btnNewButton_1.setText("解挂");
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doJg();
			}
		});

		Button btnNewButton_2 = new Button(composite, SWT.NONE);
		GridData gd_btnNewButton_2 = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_btnNewButton_2.horizontalIndent = 10;
		gd_btnNewButton_2.widthHint = 80;
		gd_btnNewButton_2.heightHint = 25;
		btnNewButton_2.setLayoutData(gd_btnNewButton_2);
		btnNewButton_2.setText("删除");
		btnNewButton_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doDelete();
			}
		});

		Button btnNewButton_3 = new Button(composite, SWT.NONE);
		GridData gd_btnNewButton_3 = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_btnNewButton_3.horizontalIndent = 10;
		gd_btnNewButton_3.widthHint = 80;
		gd_btnNewButton_3.heightHint = 25;
		btnNewButton_3.setLayoutData(gd_btnNewButton_3);
		btnNewButton_3.setText("关闭");
		btnNewButton_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doClose();
			}
		});

		DataTable zdTable = new DataTable(shell, SWT.BORDER
				| SWT.FULL_SELECTION, zdSet);
		zdTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		zdTable.setHeaderVisible(true);
		zdTable.setLinesVisible(true);

		DataTableColumn dataTableColumn_5 = new DataTableColumn(zdTable,
				SWT.NONE, "xfid");
		dataTableColumn_5.setText("帐单号");
		dataTableColumn_5.setWidth(80);

		DataTableColumn dataTableColumn_6 = new DataTableColumn(zdTable,
				SWT.NONE, "rq");
		dataTableColumn_6.setText("挂单时间");
		dataTableColumn_6.setWidth(80);

		DataTableColumn dataTableColumn_7 = new DataTableColumn(zdTable,
				SWT.NONE, "xfje");
		dataTableColumn_7.setText("消费金额");
		dataTableColumn_7.setWidth(80);

		DataTableColumn dataTableColumn_8 = new DataTableColumn(zdTable,
				SWT.NONE, "hybh");
		dataTableColumn_8.setText("会员编号");
		dataTableColumn_8.setWidth(80);

		DataTableColumn dataTableColumn_11 = new DataTableColumn(zdTable,
				SWT.NONE, "hyxm");
		dataTableColumn_11.setText("会员姓名");
		dataTableColumn_11.setWidth(80);

		DataTableColumn dataTableColumn_9 = new DataTableColumn(zdTable,
				SWT.NONE, "czy");
		dataTableColumn_9.setText("操作人");
		dataTableColumn_9.setWidth(80);

		DataTableColumn dataTableColumn_10 = new DataTableColumn(zdTable,
				SWT.NONE, "bz");
		dataTableColumn_10.setText("备注");
		dataTableColumn_10.setWidth(80);
		zdTable.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doSelected();
			}
		});

		Label lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setFont(SWTResourceManager.getFont("宋体", 9, SWT.NORMAL));
		lblNewLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		lblNewLabel.setText("帐单明细：");

		DataTable zdmxTable = new DataTable(shell, SWT.BORDER
				| SWT.FULL_SELECTION, zdmxSet);
		zdmxTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1,
				1));
		zdmxTable.setHeaderVisible(true);
		zdmxTable.setLinesVisible(true);

		DataTableColumn dataTableColumn = new DataTableColumn(zdmxTable,
				SWT.NONE, "lxmc");
		dataTableColumn.setText("项目类型");
		dataTableColumn.setWidth(80);

		DataTableColumn dataTableColumn_2 = new DataTableColumn(zdmxTable,
				SWT.RIGHT, "dj");
		dataTableColumn_2.setText("单价");
		dataTableColumn_2.setWidth(80);

		DataTableColumn dataTableColumn_3 = new DataTableColumn(zdmxTable,
				SWT.RIGHT, "cs");
		dataTableColumn_3.setText("次数");
		dataTableColumn_3.setWidth(80);

		DataTableColumn dataTableColumn_12 = new DataTableColumn(zdmxTable,
				SWT.NONE, "xmmc");
		dataTableColumn_12.setText("项目名称");
		dataTableColumn_12.setWidth(80);

		DataTableColumn dataTableColumn_13 = new DataTableColumn(zdmxTable,
				SWT.RIGHT, "zje");
		dataTableColumn_13.setText("金额");
		dataTableColumn_13.setWidth(80);
		setParams();
		doSearch();

	}

	/*
	 * 选择帐单，刷新明细
	 */
	protected void doSelected() {
		if (zdSet.getSelectedIndex() != -1) {
			setParams();
			zdmxSet.update();
		}
	}

	protected void doClose() {
		shell.close();
	}

	protected void doDelete() {
		if (zdSet.getSelectedIndex() != -1
				&& MessageDialog.openQuestion(shell, "操作确认", "确定删除单费单?")) {
			setParams();
			try {
				deleteZdBatch.execute();
			} catch (Exception e) {
				e.printStackTrace();
				MessageDialog.openError(shell, "异常", "删除失败:" + e.getMessage());
			}
		}
	}

	protected void doJg() {
		List<HyxfxmVo> list = new ArrayList<HyxfxmVo>();
		HyxfxmVo xm;
		for (int i = 0; i < zdmxSet.getCount(); i++) {
			zdmxSet.setSelected(i);
			xm = new HyxfxmVo();
			xm.setXmlx(zdmxSet.getValue("lx", Integer.class));
			xm.xmmc = zdmxSet.getString("xmmc");
			xm.xmbh = zdmxSet.getString("bh");
			xm.yj = zdmxSet.getString("xmyj");// 项目原价
			xm.dj = zdmxSet.getString("dj");
			xm.cs = zdmxSet.getValue("cs", Integer.class);
			xm.jf = zdmxSet.getString("jf");
			xm.dqcs = zdmxSet.getValue("dqcs", Integer.class);
			xm.ygbh = zdmxSet.getString("ygbh");
			xm.cw = zdmxSet.getString("cw");
			list.add(xm);
		}
		HyxfEdit edit = new HyxfEdit(hybh, list);
		FunctionDialog dlg = new FunctionDialog(shell, edit);
		dlg.open();
		if (edit.result == SWT.OK) {
			setParams();
			try {
				deleteZdBatch.execute();
				doSearch();
			} catch (Exception e) {
				e.printStackTrace();
				MessageDialog.openError(shell, "异常", "挂单删除失败，请手动删除该挂单记录" + e.getMessage());
			}
		}
	}

	protected void doSearch() {
		zdSet.setParam(0, hybh);
		zdSet.setParam(1, text.getText());
		zdSet.setParam(2, text.getText());
		zdSet.update();
		if (zdSet.getCount() > 0) {
			zdSet.setSelected(0);
			doSelected();
		}
	}

	protected void setParams() {
		deleteZdBatch.setParam(0, 0, zdSet.getValue("xfid"));
		deleteZdBatch.setParam(1, 0, zdSet.getValue("xfid"));
		zdmxSet.setParam(0, zdSet.getValue("xfid"));
	}
}
